﻿using GDGeek;
using MrPP.Myth;
using MrPP.Restful;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MrPP.SerriedCabinet
{
    public class SearchProcess : GDGeek.Singleton<SearchProcess>, IBridgeReceiver
    {



        [SerializeField]
        private SearchModel _searchModel;


        [SerializeField]
        private BoxModel _boxModel;

        private bool guiding_ = false;
        private bool searching_ = false;
        public string handle => this.longName();


        public void Start()
        {
            BridgeBroadcast.Instance.addReceiver(this);
        }
        public void OnDestory()
        {

            BridgeBroadcast.Instance.removeReceiver(this);
        }
        public Task guideTask(CabinetGroup.ItemData data) {
            return BoxProcess.Instance.open(data);
        }
        public Task searchTask(string word)
        {

            if (string.IsNullOrEmpty(word))
            {
                return new Task();
            }
            string uri = Restful.RestfulManager.Uri(UriManager.GetOrCreateInstance.getUri("api").value, "archives");
            var op = Restful.RestfulManager.Instance.options(uri, new System.Collections.Generic.Dictionary<string, string> { { "abbr", word } });
            var task = Restful.RestfulManager.Instance.getArrayTask<ArchivesData>(op, delegate (ArchivesData[] datas)
            {
                _searchModel.datas.Clear();
                for (int i = 0; i < datas.Length; ++i) {
                    _searchModel.datas.Add(CabinetGroup.Instance.getItem(datas[i]));
                }

            });
            return task;
        }


        private void doGuide()
        {
            Debug.LogError("doGuide");
            if (guiding_ == false)
            {
                if (_searchModel.selected != -1 && _searchModel.selected < _searchModel.datas.Count) {
                    _boxModel.lit = _searchModel.datas[_searchModel.selected];
                    Task task = this.guideTask(_searchModel.datas[_searchModel.selected]);
                    TaskManager.PushFront(task, delegate { guiding_ = true; });
                    TaskManager.PushBack(task, delegate { guiding_ = false; });
                    TaskManager.Run(task);
                }
                else
                {
                    _boxModel.clearLight();
                }

            }
            
        }



        private void doSearch()
        {
            if (searching_ == false)
            {
                Task task = this.searchTask(_searchModel.input);
                TaskManager.PushFront(task, delegate { searching_ = true; });
                TaskManager.PushBack(task, delegate { searching_ = false; });
                TaskManager.Run(task);
            }
        
            
        }


        public void broadcast(string evt, object data)
        {
            switch (evt)
            {
                case "search_clear":
                    _searchModel.selected = -1;
                    _searchModel.datas.Clear();
                    _boxModel.clearLight();// it = _searchModel.datas[_searchModel.selected];
                    break;

                case "open_second":
                    var lit  = new CabinetGroup.ItemData();
                    lit.type = Box.Type.None;
                    _boxModel.lit = lit;
                    if (_searchModel.isOpen == (int)data)
                    {
                        _searchModel.isOpen = -1;
                    }
                    else
                    {
                        _searchModel.isOpen = (int)data;
                        _boxModel.box = -1;
                    }

                    break;
                case "screen_guide":
                    doGuide();
                    break;
                case "screen_clear":
                    _searchModel.input = "";
                    break;
                case "screen_input":
                    _searchModel.input = (string)data;
                    break;
                case "screen_search":
                    _searchModel.selected = -1;
                    doSearch();

                    break;
                case "screen_selected":
                    int index = (int)(data);
                    _searchModel.selected = index;
                   /* if (index >= 0 && index <= _searchModel.datas.Count) {
                        var item = _searchModel.datas[index];
                        _boxModel.lit = item;
                    }
                  */
                    break;
            }
        }
    }
}